import 'dart:async';

class MyTimer {
  Timer? _timer;
  int _seconds = 0;
  bool _isRunnning = false;

  //获取当前时间字符串
  String get timeString {
    int hour = _seconds ~/ 3600;
    int minutes = (_seconds % 3600) ~/ 60;
    int secs = _seconds % 60;

    return '${hour.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
  }

//获取当前秒数
  int get seconds => _seconds;

  //获取当前运行状态
  bool get isrunning => _isRunnning;

  //开始计时
  void start() {
    if (!_isRunnning) {
      _isRunnning = true;
      _timer = Timer.periodic(Duration(seconds: 1), (timer) {
        _seconds++;
        print('当前时间: $timeString');
      });
      print('计时器已开始');
    }
  }

  //暂停计时
  void pause() {
    if (_isRunnning) {
      _isRunnning = false;
      _timer?.cancel();
      print('计时器已暂停，当前时间: $timeString');
    }
  }

  //重置计时器
  void reset() {
    _isRunnning = false;
    _timer?.cancel();
    _seconds = 0;
    print('计时器已重置');
  }

  //停止计时器
  void stop() {
    _isRunnning = false;
    _timer?.cancel();
    print('计时器已停止，最终时间：$timeString');
  }
}

//简单的计时器实例
class CountdownTimer {
  Timer? _timer;
  int _remainingSeconds;
  final int _totalSeconds;
  bool _isRunning = false;

  CountdownTimer(this._totalSeconds) : _remainingSeconds = _totalSeconds;

  String get timeString{
     int hours = _remainingSeconds ~/ 3600;
    int minutes = (_remainingSeconds % 3600) ~/ 60;
    int secs = _remainingSeconds % 60;

    return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
  }

    bool get isRunning => _isRunning;
  bool get isFinished => _remainingSeconds <= 0;
}
